import sceneContainer from "../../scene/container.js";
export async function update(stage, leftEdge, rightEdge) {
  if (this.isStop) {
    return;
  }
  this.holding = false;

  if (this.direction.isUp) {
    // 传送
    this.transfer();
  }
  // 开启物理引擎的情况下
  if (this.isPhysics) {
    if (this.direction.isRight && !this.attackLock) {
      this.phobj.hforce = this.xSpeed
    }
    if (this.direction.isLeft && !this.attackLock) {
      this.phobj.hforce = -this.xSpeed
    }
    if (this.direction.isJump && !this.attackLock) {
      this.phobj.vforce = -this.ySpeed
    }
    this.setLayer(this.phobj.fhlayer, stage)
    this.position.x = this.phobj.x.now
    this.position.y = this.phobj.y.now
    this.physics.move_object(this.phobj)
    if (this.phobj.onground) {
      this.holding = true
    }
    
  }

  // 爬梯子，需要关闭物理引擎
  if (this.direction.isLadder) {
    this.isPhysics = false
    this.stopAni();
    this.setLayer(7, stage);
    // 在梯子上向左或向右跳
    if ((this.direction.isLeft || this.direction.isRight) && this.direction.isJump) {
      this.direction.stopLadder();
      this.isPhysics = true
      this.phobj.vspeed = -this.ySpeed
      if (this.direction.isLeft) {
        this.phobj.hspeed = -this.xSpeed * 2
      } else {
        this.phobj.hspeed = this.xSpeed * 2
      }
    } else {
      if (this.direction.isUp) {
        this.startAni();
        this.position.y -= 2;
        this.position.x = this.ladderRope.getX();
        if (this.position.y <= this.ladderRope.getTop() - 2) {
          this.direction.stopLadder();
          
        }
      } else if (this.direction.isBottom) {
        this.startAni();
        this.position.x = this.ladderRope.getX();
        this.position.y += 2;
        if (this.position.y >= this.ladderRope.getBottom()) {
          this.direction.stopLadder();
          
        }
      }
    }
  } else {
    this.isPhysics = true
  }
  // let foodHolds = sceneContainer.footholdtree.footholdTree;
  // for (let item of Object.values(foodHolds)) {
  //   const { x1: l, x2: r, y1: b, y2: t, next, prev, layer, id } = item;
  //   if (this.position.x >= l && this.position.x <= r) {
  //     if (item.isGround()) {
  //       if (this.position.y >= t && this.position.y - this.ySpeed <= t) {
  //         this.position.y = t;
  //         this.fhId = id;
  //         this.setLayer(layer, stage);
  //         this.ySpeed = 2;
  //         this.jumpLock = false;
  //         this.leftJump = this.rightJump = false;
  //         this.holding = true;
  //         this.foodHold = item;
  //         this.attackedDrop = -1;
  //         break;
  //       }
  //     } else if (item.isSlope()) {
  //       let dy = item.calcY(this.position.x);
  //       if (this.position.y >= dy && this.position.y - this.ySpeed <= dy) {
  //         this.position.y = dy;
  //         this.fhId = id;
  //         this.setLayer(layer, stage);
  //         this.ySpeed = 2;
  //         this.jumpLock = false;
  //         this.leftJump = this.rightJump = false;
  //         this.holding = true;
  //         this.foodHold = item;
  //         this.attackedDrop = -1;
  //         break;
  //       }
  //     }
  //   }
  // }

  // if (this.attackedDrop === 0) {
  //   this.position.x += 1.5;
  //   let sit = this.fhId;
  //   let node = null;
  //   // 防止递归循环
  //   const length = Object.keys(foodHolds).length;
  //   let n = 0;
  //   while (sit !== 0) {
  //     if (n === length) {
  //       break;
  //     }
  //     n++;
  //     let node = foodHolds[sit];
  //     const { next } = foodHolds[sit];
  //     const l = foodHolds[sit].getLeft();
  //     const r = foodHolds[sit].getRight();
  //     const t = foodHolds[sit].getTop();
  //     const b = foodHolds[sit].getBottom();
  //     if (this.position.x - this.xSpeed <= r && this.position.x >= r) {
  //       if (node.isWall()) {
  //         if (this.position.y > t && this.position.y <= b) {
  //           this.position.x = r;
  //         }
  //       } else {
  //         this.jumpLock = true;
  //         this.rightJump = true;
  //       }
  //     }
  //     if (node.isSlope() && this.holding) {
  //       if (this.position.x >= l && this.position.x <= r) {
  //         this.position.y = node.calcY(this.position.x);
  //       }
  //     }
  //     sit = next;
  //   }
  //   return;
  // } else if (this.attackedDrop === 1) {
  //   this.position.x -= 1.5;
  //   let sit = this.fhId;
  //   let node = null;
  //   const length = Object.keys(foodHolds).length;
  //   let n = 0;
  //   while (sit !== 0) {
  //     if (n === length) {
  //       break;
  //     }
  //     n++;
  //     let node = foodHolds[sit];
  //     const { prev } = foodHolds[sit];
  //     const l = foodHolds[sit].getLeft();
  //     const r = foodHolds[sit].getRight();
  //     const t = foodHolds[sit].getTop();
  //     const b = foodHolds[sit].getBottom();
  //     if (this.position.x + this.xSpeed >= l && this.position.x <= l) {
  //       if (node.isWall()) {
  //         if (this.position.y > t && this.position.y <= b) {
  //           this.position.x = l;
  //         }
  //       } else {
  //         this.jumpLock = true;
  //         this.leftJump = true;
  //       }
  //     }
  //     if (node.isSlope() && this.holding) {
  //       if (this.position.x >= l && this.position.x <= r) {
  //         this.position.y = node.calcY(this.position.x);
  //       }
  //     }
  //     sit = prev;
  //   }
  //   return;
  // }

  // if (
  //   (this.direction.isRight || this.rightJump) &&
  //   !(this.holding && this.attackLock)
  // ) {
  //   // 在梯子上，不能移动
  //   if (!this.direction.isLadder) {
  //     this.position.x += this.xSpeed;
  //   }
  //   let sit = this.fhId;
  //   let node = null;
  //   const length = Object.keys(foodHolds).length;
  //   let n = 0;
  //   while (sit !== 0) {
  //     if (n === length) {
  //       break;
  //     }
  //     n++;
  //     let node = foodHolds[sit];
  //     const { next } = foodHolds[sit];
  //     const l = foodHolds[sit].getLeft();
  //     const r = foodHolds[sit].getRight();
  //     const t = foodHolds[sit].getTop();
  //     const b = foodHolds[sit].getBottom();
  //     if (this.position.x - this.xSpeed <= r && this.position.x > r) {
  //       if (node.isWall()) {
  //         if (this.position.y > t && this.position.y <= b) {
  //           this.position.x = r;
  //         }
  //       } else {
  //         this.jumpLock = true;
  //         this.rightJump = true;
  //       }
  //     }
  //     if (node.isSlope() && this.holding) {
  //       if (this.position.x >= l && this.position.x <= r) {
  //         this.position.y = node.calcY(this.position.x);
  //       }
  //     }
  //     sit = next;
  //   }
  // }
  // if (
  //   (this.direction.isLeft || this.leftJump) &&
  //   !(this.holding && this.attackLock)
  // ) {
  //   // 在梯子上，不能移动
  //   if (!this.direction.isLadder) {
  //     this.position.x -= this.xSpeed;
  //   }
  //   let sit = this.fhId;
  //   let node = null;
  //   const length = Object.keys(foodHolds).length;
  //   let n = 0;
  //   while (sit !== 0) {
  //     if (n === length) {
  //       break;
  //     }
  //     n++;
  //     let node = foodHolds[sit];
  //     const { prev } = foodHolds[sit];
  //     const l = foodHolds[sit].getLeft();
  //     const r = foodHolds[sit].getRight();
  //     const t = foodHolds[sit].getTop();
  //     const b = foodHolds[sit].getBottom();
  //     if (this.position.x + this.xSpeed >= l && this.position.x < l) {
  //       if (node.isWall()) {
  //         if (this.position.y > t && this.position.y <= b) {
  //           this.position.x = l;
  //         }
  //       } else {
  //         this.jumpLock = true;
  //         this.leftJump = true;
  //       }
  //     }
  //     if (node.isSlope() && this.holding) {
  //       if (this.position.x >= l && this.position.x <= r) {
  //         this.position.y = node.calcY(this.position.x);
  //       }
  //     }
  //     sit = prev;
  //   }

  //   // 防止药店跳进柜台卡bug
  //   if (this.foodHold.prev === 0) {
  //     for (let wall of sceneContainer.footholdtree.walls) {
  //       const l = wall.getLeft();
  //       const r = wall.getRight();
  //       const t = wall.getTop();
  //       const b = wall.getBottom();
  //       if (this.position.x + this.xSpeed >= l && this.position.x < l) {
  //         if (this.position.y > t && this.position.y <= b) {
  //           this.position.x = l;
  //           break;
  //         }
  //       }
  //     }
  //   }
  // }

  this.changePerformance();
  // if (this.position.x >= rightEdge) {
  //   this.position.x = rightEdge;
  // } else if (this.position.x <= leftEdge) {
  //   this.position.x = leftEdge;
  // }

  this.numUpdate();
  this.clockMap[4000].update();

  sceneContainer.mobs.forEach((mob) => {
    let attackSit = mob.attack(this);
    if (attackSit === 0 || attackSit === 1) {
      if (this.allowAttacked) {
        // 被攻击后要获取该怪物当前的地板块id，不然的话，如果能高处落下，没有接触该地板块，就无法获取
        // 当前所在的地板块。此时被攻击，就容易出界
        this.fhId = mob.fh;
        this.attacked(mob.ability.bodyAttack, attackSit);
      }
    }
  });
}
